<?php


namespace App\Models\CacheModel\zl3;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zl3OrderpayRecommend extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl3_orderpay_recommend';

    protected $cacheModel = 'orders.orderpay_recommend';

    /**
     * 更新数据
     * @param int $id
     * @param int $gid
     * @param array $data
     * @return bool
     */
    public function updateData(int $id, int $gid, array $data): bool
    {
        try
        {
            $this->where(['id' => $id])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $gid)->setCache($data);
            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Zl3OrderpayRecommend model => updateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {

        try
        {
            if ($ids)
            {   // 获取未删除 的数据
                $data = $this
                    ->where(['gid' => ['in', $ids]])->order('id asc')
                    ->column('id,img,target,link,num', 'gid');
            } else
            {
                $data = [];
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'gid', 'num']);
        }

        return $data;
    }
}
